<!DOCTYPE HTML>
<html  lang="zh" xmlns:th="http://www.thymeleaf.org">
<meta charset="utf-8">
<head th:include="include :: header"></head>
<link th:href="@{/ajax/libs/summernote/summernote.css}" rel="stylesheet"/>
<link th:href="@{/ajax/libs/summernote/summernote-bs3.css}" rel="stylesheet"/>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m"  th:object="${knowledge}" method="post" onsubmit="return false;" action="edit" id="form-knowledge-edit" enctype="multipart/form-data">
            <input id="id" name="id" th:field="*{id}"  type="hidden">
            <div class="form-group">	
                <label class="col-sm-3 control-label">标题：</label>
                <div class="col-sm-8">
                    <input id="title" name="title" th:field="*{title}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">	
                <label class="col-sm-3 control-label">封面：</label>
                <div class="col-sm-8">
                    <input id="file" name="file" class="form-control" type="file">
                    <img id="img" height="150px" width="150px"/>
                </div>
            </div>
            <div class="form-group">	
                <label class="col-sm-3 control-label">内容：</label>
                <div class="col-sm-8">
                    <!-- <input id="content" name="content" th:field="*{content}" class="form-control" type="text"> -->
                    <input id="content" name="content" th:field="*{content}" type="hidden">
                    <div id="editor" class="summernote"></div>
                </div>
            </div>
            <div class="form-group">	
                <label class="col-sm-3 control-label">状态：</label>
                <div class="col-sm-8">
                    <!-- <input id="status" name="status" th:field="*{status}" class="form-control" type="text"> -->
                    <select name="status" id="status" class="form-control m-b" th:with="type=${@dict.getType('hl_knowledge_status')}">
	                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:field="*{status}"></option>
	                </select>
                </div>
            </div>
           <!--  <div class="form-group">	
                <label class="col-sm-3 control-label">备注：</label>
                <div class="col-sm-8">
                    <input id="remark" name="remark" th:field="*{remark}" class="form-control" type="text">
                </div>
            </div> -->
            <div class="form-group">
				<div class="form-control-static col-sm-offset-9">
					<button type="submit" class="btn btn-primary">提交</button>
					<button onclick="$.modal.close()" class="btn btn-danger" type="button">关闭</button>
				</div>
			</div>
		</form>
    </div>
    <div th:include="include::footer"></div>
    <script th:src="@{/ajax/libs/summernote/summernote.min.js}"></script>
    <script th:src="@{/ajax/libs/summernote/summernote-zh-CN.js}"></script>
    <script th:inline="javascript">
	    var message = [[${knowledge.pic}]];
	</script>
    <script type="text/javascript">
		var prefix = ctx + "knowledge";
		/* console.info($("#form-knowledge-edit", window.opener.document).html()); */
		
		$(function() {
		    $('.summernote').summernote({
				height : '220px',
				lang : 'zh-CN',
				callbacks: {  
                    onImageUpload: function(files, editor, $editable) {  
                        sendFile(files[0],editor,$editable);  
                    }  
                }
		    });
			var content = $("#content").val();
		    $('#editor').code(content);
		    console.info(ctx+message);
		    $("#img").attr("src",ctx+message);
		    
		    function sendFile(file, editor, $editable){
   	            data = new FormData();
   	            data.append("file", file);
   	            var timestamp = new Date().getTime();
   	            $.ajax({
   	                data: data,
   	                type: "POST",
   	                url: prefix+"/upload", //图片上传出来的url，返回的是图片上传后的路径，http格式
   	                contentType: false,
   	                cache: false,
   	                processData: false,
   	                success: function (data) {  
   	                    var path = data.data;
   	                    $('.summernote').summernote('insertImage', path, timestamp);
   	                },  
   	                error: function () {  
   	                    alert("上传失败！");  
   	                }  
   	            });
   	        }
		    
	    });
		
		$("#form-knowledge-edit").validate({
			rules:{
				title:{
					required:true,
				},
			},
			submitHandler: function(form) {
				var sHTML = $('.summernote').code();
				$("#content").val(sHTML);
				var files = $('#file').prop('files');
	            var data = new FormData();
	            data.append('file', files[0]);
	            data.append('id', $("#id").val());
	            data.append('title', $("#title").val());
	            data.append('content',sHTML);
	            data.append('status', $("#status").val());

	            $.ajax({
	                type: 'POST',
	                url: prefix + "/edit",
	                data: data,
	                cache: false,
	                processData: false,
	                contentType: false,
	                success: function (ret) {
	                    console.info(ret);
						$.modal.close();
						window.parent.location.reload();
	                }
	            });
				/* form.submit(); */
				/* $.operate.save(prefix + "/edit", $('#form-knowledge-edit').serialize()); */
			}
		});
	</script>
</body>
</html>
